package com.nk.web;

import com.nk.domain.*;
import com.nk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {

    @RequestMapping("role")
    public String roleClick(){

        return "role";
    }

    @Autowired
    private RoleService roleService;

    @RequestMapping("getRoles")
    @ResponseBody
    public PageListRes getRoles(QueryVo vo){
        PageListRes pageListRes = roleService.getRoles(vo);
        return pageListRes;
    }

    @RequestMapping("saveRole")
    @ResponseBody
    public AjaxRes saveRole(Role role){

        AjaxRes ajaxRes = new AjaxRes();
        try {
            roleService.saveRole(role);
            ajaxRes.setSuccess(true);
            ajaxRes.setMessage("添加成功");
        }catch (Exception e){
            ajaxRes.setSuccess(false);
            ajaxRes.setMessage("添加失败");
        }

        return ajaxRes;
    }

    @RequestMapping("getPermissionByRid")
    @ResponseBody
    public List<Permission> getPermissionByRid(long rid){

        List<Permission> permissionList = roleService.getPermissionByRid(rid);

        return permissionList;
    }

    @RequestMapping("updateRole")
    @ResponseBody
    public AjaxRes updateRole(Role role){

        AjaxRes ajaxRes = new AjaxRes();
        try {
            roleService.updateRole(role);
            ajaxRes.setSuccess(true);
            ajaxRes.setMessage("修改成功");
        }catch (Exception e){
            ajaxRes.setSuccess(false);
            ajaxRes.setMessage("修改失败");
        }

        return ajaxRes;
    }

    @RequestMapping("deleteRoleByID")
    @ResponseBody
    public AjaxRes deleteRoleByID(long rid){
        AjaxRes ajaxRes = new AjaxRes();
        try {
            int res = roleService.deleteRoleByID(rid);
            if (res > 0){
                ajaxRes.setSuccess(true);
                ajaxRes.setMessage("删除成功");
            }else {
                ajaxRes.setSuccess(false);
                ajaxRes.setMessage("删除失败");
            }
        }catch (Exception e){
            ajaxRes.setSuccess(false);
            ajaxRes.setMessage("删除失败");
        }

        return ajaxRes;
    }

    @RequestMapping("selectRoles")
    @ResponseBody
    public List<Role> selectRoles(){
        List<Role> roles = roleService.selectRoles();
        return roles;
    }

    @RequestMapping("getRoleByEid")
    @ResponseBody
    public List<Long> getRoleIDByEid(Long id){

        List<Long> roleIDList = roleService.getRoleIDByEid(id);
        return roleIDList;
    }


}
